package org.luxor.common.netty.protocol.message;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import org.luxor.common.netty.protocol.NettyFixedHeader;
import org.luxor.common.netty.protocol.NettyMessageIdVariableHeader;
import org.luxor.common.netty.protocol.NettyMessageType;
import org.luxor.common.netty.protocol.NettyQoS;

/**
 * Netty PubRel Message
 * <p>
 * 客户端收到服务端(NettyPubRecMessage)签收应答后，在此向发给服务端发起的的二次确认请求消息
 *
 * @author Mr.yan @date 2024/1/31
 */
public class NettyPubRelMessage extends NettyMessage implements ByteBufHolder {

    public NettyPubRelMessage(NettyFixedHeader fixedHeader, Object variableHeader, ByteBuf payload) {
        super(fixedHeader, variableHeader, payload);
    }

    @Override
    public NettyMessageIdVariableHeader variableHeader() {
        return (NettyMessageIdVariableHeader) super.variableHeader();
    }

    @Override
    public ByteBuf payload() {
        return content();
    }

    @Override
    public ByteBuf content() {
        return ByteBufUtil.ensureAccessible((ByteBuf) super.payload());
    }

    @Override
    public NettyPubRelMessage copy() {
        return replace(content().copy());
    }

    @Override
    public NettyPubRelMessage duplicate() {
        return replace(content().duplicate());
    }

    @Override
    public NettyPubRelMessage retainedDuplicate() {
        return replace(content().retainedDuplicate());
    }

    @Override
    public NettyPubRelMessage replace(ByteBuf content) {
        return new NettyPubRelMessage(fixedHeader(), variableHeader(), content);
    }

    @Override
    public int refCnt() {
        return content().refCnt();
    }

    @Override
    public NettyPubRelMessage retain() {
        content().retain();
        return this;
    }

    @Override
    public NettyPubRelMessage retain(int increment) {
        content().retain(increment);
        return this;
    }

    @Override
    public NettyPubRelMessage touch() {
        content().touch();
        return this;
    }

    @Override
    public NettyPubRelMessage touch(Object hint) {
        content().touch(hint);
        return this;
    }

    @Override
    public boolean release() {
        return content().release();
    }

    @Override
    public boolean release(int decrement) {
        return content().release(decrement);
    }

    public static NettyPubRelMessage from(String messageId) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUB_REL, false, NettyQoS.AT_LEAST_ONCE);
        NettyMessageIdVariableHeader variableHeader = NettyMessageIdVariableHeader.from(messageId);
        return new NettyPubRelMessage(fixedHeader, variableHeader, null);
    }

}
